package com.movie2.controller;

import com.movie2.config.MyConstants;
import com.movie2.model.entity.Arrangement;
import com.movie2.model.entity.Cart;
import com.movie2.service.ArrangementService;
import com.movie2.service.CartService;
import com.movie2.service.FilmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    ArrangementService arrangementService;
    @Autowired
    FilmService filmService;

    // @GetMapping("/delete")
    // public Map<String,Object> delRecord(@RequestParam("id") int id){
    //     log.info("CartController -> delRecord(@RequestParam(\"id\") int id)");
    //     Map<String,Object> map=new HashMap<>();
    //     int count=cartService.deleteByPrimaryKey(id);
    //     if(count==1){
    //         map.put("success","success");
    //         map.put("msg","ok");
    //     }
    //     return map;
    // }

    @PostMapping("/add")
    public Map<String,Object> addToCart(@RequestBody Cart cart){
        log.info("CartController -> addToCart(@RequestBody Cart cart)");
        Map<String,Object> map=new HashMap<>();
        // SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        cart.setCreateTime(new Date());
        cart.setUpdateTime(new Date());
        int count=cartService.insert(cart);
        if(count==1){
            map.put("success","ok");
        }
        return map;
    }

    @GetMapping("")
    public List<Map<String,Object>> getCart(@RequestParam("uid") int uid){
        log.info("CartController -> getCart(@RequestParam(\"uid\") int uid)");
        List<Map<String,Object>> cartList=new ArrayList<>();
        Cart temp=new Cart();
        temp.setUid(uid);
        temp.setStatus(0);
        List<Cart> carts = cartService.selectCartsByCondition(temp);
        for (Cart cart : carts) {
            Map<String,Object> map=new HashMap<>();
            Arrangement arrangement = arrangementService.selectByPrimaryKey(cart.getAid());
            map.put("id",cart.getId());
            map.put("aid",arrangement.getId());
            map.put("status",cart.getStatus());
            String name = arrangement.getName();
            map.put("name",name);
            map.put("seats",NumToLineAndRow(cart.getSeats()));
            map.put("tel",cart.getPhone());
            map.put("start",arrangement.getStartTime());
            map.put("price",cart.getPrice());
            String bill= MyConstants.MY_URL+filmService.selectByPrimaryKey(arrangementService.selectByPrimaryKey(cart.getAid()).getFid()).getCover();
            map.put("imgAddress",bill);
            cartList.add(map);
        }
        return cartList;

    }

    public List<Map<String,Object>> NumToLineAndRow(String str){
        List<Map<String,Object>> mapList=new ArrayList<>();
        String [] tag=str.split(",");
        for (String s : tag) {
            Map<String,Object> map=new HashMap<>();
            int num=Integer.parseInt(s);
            int line= (int)Math.ceil((double) num/12);

            int row=0;
            if(num%12==0){
                row=12;
            }else{
                row=(int) num%12;
            }
            // System.out.println(num);
            // System.out.println(line);
            // System.out.println(row);
            // System.out.println("==========");
            map.put("line",line);
            map.put("row",row);
            mapList.add(map);
        }
        return mapList;
    }


}
